﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.Control
{
    public partial class UsrMenuLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            rptMenuParent.DataSource = A.Controller.BackEndMenuController.GetAllBackEndMenuParent();
            rptMenuParent.DataBind();
        }

        protected void rptMenuParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptMenuChild = (Repeater) e.Item.FindControl("rptMenuChild");
                rptMenuChild.DataSource =
                    A.Controller.BackEndMenuController.GetAllBackEndMenuChildByParentId(DataBinder.Eval(
                        e.Item.DataItem, "Id"));
                rptMenuChild.DataBind();
            }
        }

        protected string GetAllPage(object menuId)
        {
            var list = A.Controller.BackEndPageController.GetAllPageByMenuId(menuId);
            string result = string.Empty;
            foreach (var item in list)
            {
                if (Request.Url.AbsolutePath.Equals(item.AbsolutePath))
                    result += "<li class='active'><a href='" + item.AbsolutePath +"'>" + item.Name + "</a></li>";
                else
                    result += "<li><a href='" + item.AbsolutePath + "'>" + item.Name + "</a></li>";
            }
            return result;
        }

        protected string Display(object parentId)
        {
            BackEndPage backEndPage = A.Controller.BackEndPageController.GetBackEndMenuPageCurrent();
            if (backEndPage == null)
                return "display:none";
            if (backEndPage.ParentId == null && backEndPage.BackEndMenu.BackEndMenu1.Id == Convert.ToInt32(parentId))
                return string.Empty;
            if (backEndPage.ParentId != null && backEndPage.BackEndPage1.BackEndMenu.BackEndMenu1.Id == Convert.ToInt32(parentId))
                return string.Empty;
            return "display:none";
        }
    }
}